package com.yzc.android.ui.base.fragment;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.yzc.android.R;
import com.yzc.android.widget.RentalsSunHeaderView;
import com.yzc.android.widget.TitleHeaderBar;

import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.header.MaterialHeader;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

/**
 * Created by yzc on 16/4/25.
 */
public abstract class NavBarBaseFragment extends KDFragment {

    protected TitleHeaderBar mNavigationBar;

    protected PtrFrameLayout mPtrFrameLayout;
    protected FrameLayout mContainerLayout;
    protected PtrUIHandler mDefaultHeader;

    public abstract @LayoutRes int getLayout();

    // 下拉刷新
    public abstract void pullToRefreshHandler();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_activity_base_navbar, null);
        mNavigationBar = (TitleHeaderBar) view.findViewById(R.id.navigation_bar);
        mContainerLayout = (FrameLayout) view.findViewById(R.id.main_view_container);
        mPtrFrameLayout = (PtrFrameLayout) view.findViewById(R.id.main_view_ptr_frame_layout);
        View contentView = inflater.inflate(getLayout(), null);
        mContainerLayout.addView(contentView);
        ButterKnife.bind(this, view);

        if (enableDefaultBack()) {
            mNavigationBar.setLeftOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        } else {
            mNavigationBar.getLeftViewContainer().setVisibility(View.INVISIBLE);
        }

        if (enablePullRefresh()) {
            loadDefaultRefreshHeader();
        }
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void loadDefaultRefreshHeader()
    {
        PtrClassicDefaultHeader defaultHeader = new PtrClassicDefaultHeader(getContext());
        mPtrFrameLayout.removePtrUIHandler(mDefaultHeader);
        mPtrFrameLayout.setHeaderView(defaultHeader);
        mPtrFrameLayout.addPtrUIHandler(defaultHeader);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header) {
                boolean canDoPullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                return canDoPullDown && enablePullRefresh();
            }

            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                        pullToRefreshHandler();
                    }
                }, 1800);
            }
        });
        mDefaultHeader = defaultHeader;
    }

    private void loadStoreHouseRefreshHeader()
    {
        StoreHouseHeader houseHeader = new StoreHouseHeader(getContext());

        //houseHeader.initWithString("XCode Big Air");
        houseHeader.initWithStringArray(R.array.akta);
        mPtrFrameLayout.removePtrUIHandler(mDefaultHeader);
        mPtrFrameLayout.setHeaderView(houseHeader);
        mPtrFrameLayout.addPtrUIHandler(houseHeader);
        mPtrFrameLayout.autoRefresh(false);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header) {
                boolean canDoPullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                return canDoPullDown && enablePullRefresh();
            }

            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                        pullToRefreshHandler();
                    }
                }, 1800);
            }
        });
        mDefaultHeader = houseHeader;
    }

    private void loadMaterialRefreshHeader()
    {
        MaterialHeader materialHeader = new MaterialHeader(getContext());
        int[] colors = getResources().getIntArray(R.array.google_colors);
        materialHeader.setColorSchemeColors(colors);
        materialHeader.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        materialHeader.setPadding(0, 15, 0, 10);
        materialHeader.setPtrFrameLayout(mPtrFrameLayout);
        mPtrFrameLayout.removePtrUIHandler(mDefaultHeader);
        mPtrFrameLayout.setHeaderView(materialHeader);
        mPtrFrameLayout.addPtrUIHandler(materialHeader);
        mPtrFrameLayout.autoRefresh(false);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header) {
                boolean canDoPullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                return canDoPullDown && enablePullRefresh();
            }

            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                        pullToRefreshHandler();
                    }
                }, 1800);
            }
        });
        mDefaultHeader = materialHeader;
    }

    private void loadRentalRefreshHeader() {
        RentalsSunHeaderView rentalsSunHeaderView = new RentalsSunHeaderView(getContext());
        rentalsSunHeaderView.setUp(mPtrFrameLayout);
        mPtrFrameLayout.removePtrUIHandler(mDefaultHeader);
        mPtrFrameLayout.setHeaderView(rentalsSunHeaderView);
        mPtrFrameLayout.addPtrUIHandler(rentalsSunHeaderView);
        mPtrFrameLayout.autoRefresh(false);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header) {
                boolean canDoPullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                return canDoPullDown && enablePullRefresh();
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                ptrFrameLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                        pullToRefreshHandler();
                    }
                }, 1800);
            }
        });
        mDefaultHeader = rentalsSunHeaderView;
    }

    /**
     * make it looks like Activity
     */
    protected void onBackPressed() {
        getContext().onBackPressed();
    }

    protected boolean enableDefaultBack() {
        return true;
    }

    protected boolean enablePullRefresh() {
        return false;
    }

    protected void setHeaderTitle(int id) {
        mNavigationBar.getTitleTextView().setText(id);
    }

    protected void setHeaderTitle(String title) {
        mNavigationBar.getTitleTextView().setText(title);
    }
}
